﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AuthoSYS.EF;

namespace AuthoSYS.Web.Controllers
{
    public class RoleController : Controller
    {
        private AuthoDBEntities db = new AuthoDBEntities();

        //
        // GET: /Role/

        public ActionResult Index(string appId)
        {
            ViewBag.AppId = appId;
            return View(db.Roles.Include("Application").Where(r => r.AppId == appId).ToList());
        }

        //
        // GET: /Role/Details/5

        public ActionResult Details(string id = null)
        {
            Role role = db.Roles.Single(r => r.Id == id);
            if (role == null)
            {
                return HttpNotFound();
            }
            return View(role);
        }

        //
        // GET: /Role/Create

        public ActionResult Create(string appId)
        {
            Role model = new Role { Id = Guid.NewGuid().ToString(), AppId = appId };
            return View(model);
        }

        //
        // POST: /Role/Create

        [HttpPost]
        public ActionResult Create(Role role, string[] permissionValues)
        {
            if (ModelState.IsValid)
            {
                role.Id = Guid.NewGuid().ToString();
                role.CreatedOn = DateTime.Now;
                role.ModifiedOn = DateTime.Now;
                role.Permissions = Request["permissionValues"].ToString();

                db.Roles.AddObject(role);
                db.SaveChanges();
                return RedirectToAction("Index", new { appId = role.AppId });
            }

            return View(role);
        }

        //
        // GET: /Role/Edit/5

        public ActionResult Edit(string id = null)
        {
            Role role = db.Roles.Single(r => r.Id == id);
            if (role == null)
            {
                return HttpNotFound();
            }
            return View(role);
        }

        //
        // POST: /Role/Edit/5

        [HttpPost]
        public ActionResult Edit(Role role)
        {
            if (ModelState.IsValid)
            {
                role.Permissions = Request["permissionValues"].ToString();
                db.Roles.Attach(role);
                db.ObjectStateManager.ChangeObjectState(role, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(role);
        }

        //
        // GET: /Role/Delete/5

        public ActionResult Delete(string id = null)
        {
            Role role = db.Roles.Single(r => r.Id == id);
            if (role == null)
            {
                return HttpNotFound();
            }
            return View(role);
        }

        //
        // POST: /Role/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(string id)
        {
            Role role = db.Roles.Single(r => r.Id == id);
            db.Roles.DeleteObject(role);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        public ActionResult GetRolesByAppId(string roles, string appId)
        {
            IEnumerable<SelectListItem> roleList = new List<SelectListItem>();

            if (!string.IsNullOrEmpty(appId))
            {
                if (string.IsNullOrEmpty(roles))
                {
                    roleList = db.Roles.Where(r => r.AppId == appId).Select(r => new SelectListItem { Text = r.RoleName, Value = r.Id }).ToList();
                }
                else
                {
                    var selectedRoles = roles.Split(',');
                    roleList = db.Roles.Where(r => r.AppId == appId).Select(r => new SelectListItem { Text = r.RoleName, Value = r.Id, Selected = selectedRoles.Contains(r.Id) }).ToList();
                }
            }


            return PartialView("_RolesByAppIdPartialView", roleList);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}